package com.example.day1_dialog;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class MusicAdapter extends BaseAdapter {
    List<MusicEntity.DataBean> list = new ArrayList<>();
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_music,parent,false);
            convertView.setTag(new MyViewHolder(convertView));
        }
        MyViewHolder viewHolder = (MyViewHolder) convertView.getTag();
        viewHolder.tv.setText(list.get(position).getName());
        Glide.with(parent.getContext()).load(list.get(position).getPicUrl()).into(viewHolder.iv);
        return convertView;
    }

    public void addAll(List<MusicEntity.DataBean> data){
        list.addAll(data);
        notifyDataSetChanged();
    }
    public void remove(int position){
        list.remove(position);
        notifyDataSetChanged();
    }

    class MyViewHolder{
        ImageView iv;
        TextView tv;
        public MyViewHolder(View view) {
            iv = view.findViewById(R.id.iv);
            tv = view.findViewById(R.id.tv);
        }
    }
}
